/**
 * 
 */
package com.huawei.struts.service.imp;

import java.util.List;

import com.huawei.struts.dao.LjwProgramItemTDAO;
import com.huawei.struts.dao.LjwProgramTDAO;
import com.huawei.struts.exception.SystemException;
import com.huawei.struts.service.IProgramService;
import com.huawei.struts.util.PageBean;
import com.st.model.LjwProgramItemT;
import com.st.model.LjwProgramT;

/**
 * @author Administrator
 *
 */
public class ProgramService implements IProgramService {

	private LjwProgramTDAO lwjProgramDao;
	private LjwProgramItemTDAO ljwProgramItemTDAO;
	
	
	public void setLjwProgramItemTDAO(LjwProgramItemTDAO ljwProgramItemTDAO) {
		this.ljwProgramItemTDAO = ljwProgramItemTDAO;
	}

	public void setLwjProgramDao(LjwProgramTDAO lwjProgramDao) {
		this.lwjProgramDao = lwjProgramDao;
	}

	/* (non-Javadoc)
	 * @see com.huawei.struts.service.IProgramService#addProgram(com.st.model.LjwProgramT)
	 */
	public void addProgram(LjwProgramT program,String village,String villageName,String channel,String channelName) throws SystemException {
		try{
			Object obj= lwjProgramDao.save(program);//第一步 添加数据到program表
			Integer programId=Integer.valueOf(obj.toString());//得到序列
			if(village!=null && !"".equals(village)){
				if(village.equals("@allConditions@")){
					LjwProgramItemT programItem=new LjwProgramItemT();
					String value=village;
					String displayValue=villageName;
					programItem.setValue(value);
					programItem.setName("village");
					programItem.setProgramId(programId);
					programItem.setDisplayValue(displayValue);
					ljwProgramItemTDAO.save(programItem);//如果选择是 all第二步 添加小区的数据到program_item表
				}
				else{
					String[] villages=village.split(",");
					String[] villageNames=villageName.split(",");
					for(int i=0;i<villages.length;i++){
						LjwProgramItemT programItem=new LjwProgramItemT();
						String value=villages[i];
						String displayValue=villageNames[i];
						programItem.setValue(value);
						programItem.setName("village");
						programItem.setProgramId(programId);
						programItem.setDisplayValue(displayValue);
						ljwProgramItemTDAO.save(programItem);//第二步 添加小区的数据到program_item表
					}
				}
			}
			if(channel!=null && !"".equals(channel)){
				if(channel.equals("@allConditions@")){
					LjwProgramItemT programItem=new LjwProgramItemT();
					String value=channel;
					String displayValue=channelName;
					programItem.setValue(value);
					programItem.setName("channel");
					programItem.setProgramId(programId);
					programItem.setDisplayValue(displayValue);
					ljwProgramItemTDAO.save(programItem);//如果选择 是all第三步 添加频道的数据到program_item表
				}
				else{
					String[] channels=channel.split(",");
					String[] channelNames=channelName.split(",");
					for(int j=0;j<channels.length;j++){
						LjwProgramItemT programItem=new LjwProgramItemT();
						String value=channels[j];
						String displayValue=channelNames[j];
						programItem.setValue(value);
						programItem.setName("channel");
						programItem.setProgramId(programId);
						programItem.setDisplayValue(displayValue);
						ljwProgramItemTDAO.save(programItem);//第三步 添加频道的数据到program_item表
					}
				}
				
			}
			 
		} catch (Exception e) {
			throw new SystemException("error.info", e.getCause());
		}

	}

	/* (non-Javadoc)
	 * @see com.huawei.struts.service.IProgramService#delProgram(com.st.model.LjwProgramT)
	 */
	public void delProgram(LjwProgramT program) throws SystemException {
		try{
			lwjProgramDao.delete(program);
		} catch (Exception e) {
			throw new SystemException("error.info", e.getCause());
		}

	}

	/* (non-Javadoc)
	 * @see com.huawei.struts.service.IProgramService#getProgramInfoByid(java.lang.Integer)
	 */
	public LjwProgramT getProgramInfoByid(Integer id) throws SystemException {
		LjwProgramT programVo=new LjwProgramT(); 
		LjwProgramT program=lwjProgramDao.findById(id);
		// 小区
		LjwProgramItemT item1=new LjwProgramItemT();
		item1.setProgramId(program.getProgramId());
		item1.setName("village");
		List<LjwProgramItemT> list=ljwProgramItemTDAO.findByExample(item1);
		StringBuffer villageSB=new StringBuffer();
		StringBuffer villageNameSB=new StringBuffer();
		for(int i=0;i<list.size();i++){
			LjwProgramItemT programItem=list.get(i);
			if(i==0){
				villageSB.append(programItem.getValue());
				villageNameSB.append(programItem.getDisplayValue());
			}else{
				villageSB.append(","+programItem.getValue());
				villageNameSB.append(","+programItem.getDisplayValue());
			}
		}
		
		//频道
		LjwProgramItemT item2=new LjwProgramItemT();
		item2.setProgramId(program.getProgramId());
		item2.setName("channel");
		List<LjwProgramItemT> list2=ljwProgramItemTDAO.findByExample(item2);
		StringBuffer channelSB=new StringBuffer();
		StringBuffer channelNameSB=new StringBuffer();
		for(int i=0;i<list2.size();i++){
			LjwProgramItemT programItem=list2.get(i);
			if(i==0){
				channelSB.append(programItem.getValue());
				channelNameSB.append(programItem.getDisplayValue());
			}else{
				channelSB.append(","+programItem.getValue());
				channelNameSB.append(","+programItem.getDisplayValue());
			}
		}
		programVo.setVillage(villageSB.toString());
		programVo.setVillageName(villageNameSB.toString());
		programVo.setChanenl(channelSB.toString());
		programVo.setChannelName(channelNameSB.toString());
		programVo.setProgramName(program.getProgramName());
		programVo.setDescription(program.getDescription());
		programVo.setProgramId(program.getProgramId());
		return programVo;
	}

	/* (non-Javadoc)
	 * @see com.huawei.struts.service.IProgramService#getProgramList(com.st.model.LjwProgramT, com.huawei.struts.util.PageBean)
	 */
	public PageBean getProgramList(LjwProgramT program, PageBean pageBean) {
		int allRows=lwjProgramDao.getAllRowCount(program);//得到总记录数
		pageBean=pageBean.pagationTemplate(allRows, pageBean.getCurrentPage());//获取分页的全部信息
		List list=lwjProgramDao.queryForPage(program, pageBean);//得到结果集
		pageBean.setList(list);
		return pageBean;
	}

	/* (non-Javadoc)
	 * @see com.huawei.struts.service.IProgramService#modifyProgram(com.st.model.LjwProgramT)
	 */
	public void modifyProgram(LjwProgramT program,String village,String villageName,String channel,String channelName) throws SystemException {
		try{
			lwjProgramDao.merge(program);//修改Program 表
			
			Object obj= program.getProgramId();//得到program id
			Integer programId=Integer.valueOf(obj.toString());//转换类型
			
			ljwProgramItemTDAO.deleteProgromByProgramId(programId);//通过programid 旧删除
			
			if(village!=null && !"".equals(village)){
				if(village.equals("@allConditions@")){
					LjwProgramItemT programItem=new LjwProgramItemT();
					String value=village;
					String displayValue=villageName;
					programItem.setValue(value);
					programItem.setName("village");
					programItem.setProgramId(programId);
					programItem.setDisplayValue(displayValue);
					ljwProgramItemTDAO.save(programItem);//如果选择是 all第二步 添加小区的数据到program_item表
				}
				else{
					String[] villages=village.split(",");
					String[] villageNames=villageName.split(",");
					for(int i=0;i<villages.length;i++){
						LjwProgramItemT programItem=new LjwProgramItemT();
						String value=villages[i];
						String displayValue=villageNames[i];
						programItem.setValue(value);
						programItem.setName("village");
						programItem.setProgramId(programId);
						programItem.setDisplayValue(displayValue);
						ljwProgramItemTDAO.save(programItem);//第二步 添加小区的数据到program_item表
					}
				}
			}
			if(channel!=null && !"".equals(channel)){
				if(channel.equals("@allConditions@")){
					LjwProgramItemT programItem=new LjwProgramItemT();
					String value=channel;
					String displayValue=channelName;
					programItem.setValue(value);
					programItem.setName("channel");
					programItem.setProgramId(programId);
					programItem.setDisplayValue(displayValue);
					ljwProgramItemTDAO.save(programItem);//如果选择 是all第三步 添加频道的数据到program_item表
				}
				else{
					String[] channels=channel.split(",");
					String[] channelNames=channelName.split(",");
					for(int j=0;j<channels.length;j++){
						LjwProgramItemT programItem=new LjwProgramItemT();
						String value=channels[j];
						String displayValue=channelNames[j];
						programItem.setValue(value);
						programItem.setName("channel");
						programItem.setProgramId(programId);
						programItem.setDisplayValue(displayValue);
						ljwProgramItemTDAO.save(programItem);//第三步 添加频道的数据到program_item表
					}
				}
				
			}
		} catch (Exception e) {
			throw new SystemException("error.info", e.getCause());
		}

	}

}
